package com.java.onlineshop.mvc.controller;

import com.java.onlineshop.mvc.handler.OrderHandler;
import com.java.onlineshop.mvc.handler.ProductHandler;
import com.java.onlineshop.mvc.handler.ShopCartHandler;
import com.java.onlineshop.mvc.handler.UserHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @program: onlineshop
 * @description: 总控
 * @author: ShuangChenYue
 * @create: 2021-05-28 11:51
 **/
@WebServlet(value = "*.do")
public class ControllerServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException
    {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException
    {
        UserHandler userHandler = new UserHandler();
        ProductHandler productHandler = new ProductHandler();
        ShopCartHandler shopCartHandler = new ShopCartHandler();
        OrderHandler orderHandler = new OrderHandler();
        String servletPath = req.getServletPath();
        servletPath=servletPath.substring(0,servletPath.lastIndexOf("."));
        if ("/mvc/toRegister".equals(servletPath))
        {
            userHandler.toRegister(req, resp);
        }else if ("/mvc/userRegister".equals(servletPath))
        {
            userHandler.userRegister(req, resp);
        }else if ("/mvc/userLogin".equals(servletPath))
        {
            userHandler.userLogin(req, resp);
        }else if ("/mvc/toUpdate".equals(servletPath))
        {
            userHandler.toUpdate(req, resp);
        }else if ("/mvc/updateUser".equals(servletPath))
        {
            userHandler.updateUser(req, resp);
        }else if ("/mvc/logout".equals(servletPath))
        {
            userHandler.logout(req, resp);
        }else if ("/mvc/showProductList".equals(servletPath))
        {
            productHandler.showProductList(req, resp);
        }else if ("/mvc/showProductDetail".equals(servletPath))
        {
            productHandler.showProductDetail(req, resp);
        }else if ("/mvc/showProductBySearch".equals(servletPath))
        {
            productHandler.showProductBySearch(req, resp);
        }else if ("/mvc/showLoginProductDetail".equals(servletPath))
        {
            productHandler.showLoginProductDetail(req, resp);
        }else if ("/mvc/showLoginProductBySearch".equals(servletPath))
        {
            productHandler.showLoginProductBySearch(req, resp);
        }else if ("/mvc/showProductByCategoryId".equals(servletPath))
        {
            productHandler.showProductByCategoryId(req, resp);
        }else if ("/mvc/showLoginProductByCategoryId".equals(servletPath))
        {
            productHandler.showLoginProductByCategoryId(req, resp);
        }else if ("/mvc/addToCart".equals(servletPath))
        {
            shopCartHandler.addToCart(req, resp);
        }else if ("/mvc/viewCart".equals(servletPath))
        {
            shopCartHandler.viewCart(req, resp);
        }else if ("/mvc/deleteById".equals(servletPath))
        {
            shopCartHandler.deleteById(req, resp);
        }else if ("/mvc/clearCart".equals(servletPath))
        {
            shopCartHandler.clearCart(req, resp);
        }else if ("/mvc/subAmount".equals(servletPath))
        {
            shopCartHandler.subAmount(req, resp);
        }else if ("/mvc/addAmount".equals(servletPath))
        {
            shopCartHandler.addAmount(req, resp);
        }else if ("/mvc/toOrder".equals(servletPath))
        {
            orderHandler.toOrder(req, resp);
        }else if ("/mvc/submitOrder".equals(servletPath))
        {
            orderHandler.submitOrder(req, resp);
        }
    }
}
